define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"http://api.tianditu.gov.cn/api?v=4.0&tk=aeb17a8215786a5dd17f5f9c92bebc0c",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(Util, Tool, SelectOrg, SearchBlock){

	var map = null;
	// 矩形绘制工具
	var rectTool = null;
	//查询的所有数据
	var featureList = [];

	var getQhDrillOverviewController = function(){
		return getServer() + "/qh/qhDrillOverviews";
	};

	/**
	 * 页面初始化
	 */
	var init = function(){
		$("#searchButton").click(searchMarker);
		initMap();
		getDrillOverviewData();
	};

    var getDrillOverviewData = function () {
        $.ajax({
            url: getQhDrillOverviewController() + "/queryQhDrillOverviewsNoPage",
			contentType:"application/json",
			type: "post",
			data:JSON.stringify({}),
            success: function (datas) {
				datas.data.forEach(function (feature) {
					var lng = parseFloat(feature.longitude);
					var lat = parseFloat(feature.latitude);
					if(isNaN(lng) || isNaN(lat) || lng > 180 || lng < -180 || lat > 90 || lat < -90) {
						return;
					}
					feature.lnglat = new T.LngLat(lng,lat);
					featureList.push(feature);
				})
            },
            pagination: true,
            pageSize: 10
        });
	};

    var searchMarker = function () {
    	if(!rectTool) { return; }
    	rectTool.clear();
		rectTool.open();
	};
    
    var endRectDraw = function (params) {
		if(!rectTool) { return; }
		rectTool.close();
		map.clearOverLays();
		addMarkers(params.currentBounds);
		rectTool.clear();
	};

    var addMarkers = function(bounds) {
    	for(var i = 0; i < featureList.length; ++i) {
    		var feature = featureList[i];
    		if(bounds.contains(feature.lnglat)) {
				//创建标注对象
				var marker = new T.Marker(feature.lnglat);
				marker.drillCode = feature.drillCode;
				marker.reportName = feature.reportName;
				marker.projectName = feature.projectName;
				marker.siteClass = feature.siteClass;
				marker.lng = feature.longitude;
				marker.lat = feature.latitude;
				marker.id = feature.id;
				map.addOverLay(marker);
				marker.addEventListener("click", markerClick);
			}
		}
	};

	var markerClick = function (e) {
		var infoWin1 = new T.InfoWindow();
		var sContent =
			//"<div style='margin:0px;width:400px'>" +
			"<table style='border:1px solid #98bf21;padding:3px 7px 2px 7px;border-collapse:collapse'>" +
			"<tr>" +
			"<td>钻孔编号</td>" +
			"<td>" + (this.drillCode || "") + "</td>" +
			"<td>资料报告名称</td>" +
			"<td>" + (this.reportName || "") + "</td>" +
			"</tr>" +
			"<tr>" +
			"<td>勘察工程名称</td>" +
			"<td>" + (this.projectName || "") + "</td>" +
			"<td>场地分类</td>" +
			"<td>" + (this.siteClass || "") + "</td>" +
			"</tr>" +
			"<tr>" +
			"<td>钻孔坐标经度值</td>" +
			"<td>" + (this.lng || "") + "</td>" +
			"<td>钻孔坐标纬度值</td>" +
			"<td>" + (this.lat || "") + "</td>" +
			"</tr>" +
			"<tr>" +
			"<td colspan='2'><a id='drillBtn1' >钻孔概况</a></td>" +
			"<td colspan='2'><a id='drillBtn2'>钻孔综合柱状图</a></td>" +
			"</tr>" +
			"<tr>" +
			"<td colspan='2'><a id='drillBtn3'>标准贯入原位测试结果</a></td>" +
			"<td colspan='2'><a id='drillBtn4'>剪切波速测试结果表</a></td>" +
			"</tr>" +
			"<tr>" +
			"<td colspan='2'><a id='drillBtn5'>土动力测试数据</a></td>" +
			"<td colspan='2'><a id='drillBtn6'>抗震设防参数</a></td>" +
			"</tr>" +
			"</table>";// +
		//"</div>";
		infoWin1.setContent(sContent);
		this.openInfoWindow(infoWin1);

		var drillId = this.drillCode;
		for(var i = 1; i < 7; ++i) {
			(function (i) {
				var buttonName = '#drillBtn' + i;

				$(buttonName).click(function(e){
					// var type = e.target.id.;
					toPage(i,drillId);
				});
			})(i);

		}
	};

	var toPage = function(type, id) {
		window.parent.jumpToWithId(type,id);
	};

	var initMap = function() {
		//初始化地图对象
		var config = {
			projection: "EPSG:4326",
			minZoom: 4
		};

		map = new T.Map("mapDiv",config);
		//设置显示地图的中心点和级别
		map.centerAndZoom(new T.LngLat(116.40969,39.89945),5);

		var control=new T.Control.Zoom();
		map.addControl(control);

		//创建比例尺控件对象
		var scale = new T.Control.Scale();
		map.addControl(scale);

		//创建鹰眼控件对象
		var miniMap = new T.Control.OverviewMap({
			isOpen: true,
			size: new T.Point(150, 150)
		});
		map.addControl(miniMap);

		//创建地图类型控件对象
		var ctrl = new T.Control.MapType();
		map.addControl(ctrl);

		rectTool = new T.RectangleTool(map);

		rectTool.addEventListener("draw",endRectDraw);

		var height = window.innerHeight - 15;
		var width = window.innerWidth - 15;

		$("#mapDiv").css("height",height);
		$("#mapDiv").css("width",width);

		map.checkResize();
	};
	return {
		init:init
	};
});
